﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace System.Numerics
{
	/// <summary>
	/// Extends <see cref="Complex"/>
	/// </summary>
	public static class ExtensionComplex
	{
		private const char ImaginaryConstantChar = 'i';
		public static string ToString(this Complex c, string Format)
		{
			if (Format.StartsWith("c", StringComparison.OrdinalIgnoreCase) == true)
				return ExtensionComplex.ToStringCarthes(c);
			if (Format.StartsWith("p", StringComparison.OrdinalIgnoreCase) == true)
				return ExtensionComplex.ToStringPolar(c);
			if (Format.StartsWith("e", StringComparison.OrdinalIgnoreCase) == true)
				return ExtensionComplex.ToStringExponential(c);

			throw new Exception("Invalid Formatstring");
		}

		private static string ToStringCarthes(Complex c)
		{
			String text = c.Real.ToString();
			char sign;
			double im;
			if (c.Imaginary < 0)
			{
				sign = '-';
				im = -c.Imaginary;
			}
			else
			{
				sign = '+';
				im = c.Imaginary;
			}
			text += " " + sign + " " + im.ToString() + ExtensionComplex.ImaginaryConstantChar;
			return text;
		}

		private static string ToStringPolar(Complex c)
		{
			throw new NotImplementedException();
		}

		private static string ToStringExponential(Complex c)
		{
			throw new NotImplementedException();
		}

		//public static Complex Parse(string Text)
		//{
		//	int i = Text.IndexOfAny(new Char[] { '+', '-' });
		//	double re = 0;
		//	double im = 0;

		//	if (i < 0)
		//	{
		//		if (Text.EndsWith(ExtensionComplex.ImaginaryConstantChar.ToString()) == true)
		//			im = double.Parse(Text.Substring(Text.Length - 1));
		//		else
		//			re = double.Parse(Text);
		//		return new Complex(re, im);
		//	}

		//	if (i == 0 || Text[i-1] == 'e' || Text[i-1] == 'E')
		//		i = Text.IndexOfAny(new Char[] { '+', '-' }, i+1);

		//	string sRe = Text.Substring(0, i);
		//	string sIm = Text.Substring(i + 1);
		//}

		public static Point ToPoint(this Complex c)
		{
			return new Point(c.Real, c.Imaginary);
		}
	}
}
